package think.thread.c07;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class BeeperControl {
    private final ScheduledExecutorService scheduler = 
       Executors.newScheduledThreadPool(1);

    public void beepForAnHour() {
        final Runnable beeper = new Runnable() {
                public void run() { System.out.println("beep"); }
            };
        final ScheduledFuture<?> beeperHandle = 
            scheduler.scheduleAtFixedRate(beeper, 1, 1, TimeUnit.SECONDS);
        scheduler.schedule(new Runnable() {
                public void run() { 
                	beeperHandle.cancel(true); 
                	try {
                		TimeUnit.SECONDS.sleep(3);
                    	System.out.println("cancel");
					} catch (Exception e) {
					}
                }
            }, 2, TimeUnit.SECONDS);
//        scheduler.shutdown();
    }
    
    public static void main(String[] args) {
		new BeeperControl().beepForAnHour();
	}
}